import pandas as pd
import numpy as np
from datetime import datetime
from decimal import *


def time2stamp(cmnttime):  # 转时间戳函数

    if pd.isnull(cmnttime):
        return 0
    cmnttime = datetime.strptime(cmnttime, '%m/%d/%Y')
    stamp = int(datetime.timestamp(cmnttime))
    return stamp


def cal_vol_diff(today_vol, pre_vol):
    return (today_vol - pre_vol) / pre_vol * 100

database = pd.read_csv('database.csv', '\t')

# 清除空值
database = database.dropna(axis=0, how='any')

# 转换时间戳
database['timestamp'] = database['date'].apply(time2stamp)

# 排序 ascending 是否升序
database = database.sort_values(by='timestamp', ascending=True)
# database =  database.sort_index(by='timestamp', ascending=False)
database = database.reset_index(drop=True)
# 重排索引
# database.reset_index(drop = True)


# 列出昨天的相关数据
database['vol_yesterday'] = database['volume'].shift(1)
database['open_yesterday'] = database['open'].shift(1)
database['high_yesterday'] = database['high'].shift(1)
database['low_yesterday'] = database['low'].shift(1)
database['close_yesterday'] = database['close'].shift(1)

# database['vol-'] = database.apply(lambda x: cal_vol_diff(x['volume'], x['vol_yesterday']), axis=1)
close_list = database['close']

# data_edata = pd.DataFrame(columns=['date','index','open','high','low','close','yesterday_close_ema5'])
data_edata = []

def c_occhn(close_ema, openema):
    return (close_ema - openema) / openema * 10000

def c_ccchn(today_close_ema, yesterday_close_ema):
    return (today_close_ema-yesterday_close_ema)/yesterday_close_ema*10000

def c_cc_dis3():
    return (get_yesterday_ema(5) -close) /close * 10000

def c_cc_dis13():
    return  (get_yesterday_ema(5)-get_yesterday_ema(21))/get_yesterday_ema(21)*10000

def c_cc_dis34():
    return  (get_yesterday_ema(55)-get_yesterday_ema(21))/get_yesterday_ema(21)*10000

def c_ema_open(num):
    if num == 5:
        alpha = 0.33
    elif num == 21:
        alpha = 0.09
    elif num == 55:
        alpha = 0.04

    if index == 0:
        ema_open_result = _open
    else:
        attr_ket = "ema_open_{}".format(num)
        yesterday_open_ema = data_edata[index - 1][attr_ket]
        ema_open_result = _open * Decimal.from_float(alpha) + yesterday_open_ema * Decimal.from_float(1 - alpha)
    return ema_open_result.quantize(Decimal("0.00"))



def c_ema_close(num):
    if num == 5:
        alpha = 0.33
    elif num == 21:
        alpha = 0.09
    elif num == 55:
        alpha = 0.04

    if index == 0:
        ema_close_result = close
    else:
        attr_ket = "ema_close_{}".format(num)
        yesterday_close_ema = data_edata[index - 1][attr_ket]

        ema_close_result = close * Decimal.from_float(alpha) + yesterday_close_ema * Decimal.from_float(1- alpha)
    return ema_close_result.quantize(Decimal("0.00"))

def get_yesterday_ema(num):
    if index == 0:
        return close
    else:
        attr_ket = "ema_close_{}".format(num)
        yesterday_close_ema = data_edata[index - 1][attr_ket]
        return yesterday_close_ema

def c_vol_sub():
    if index == 0:
        vol_sub = 0
    else:
        yesterday_vol = data_edata[index - 1]['vol']
        vol_sub = float(vol) - float(yesterday_vol)
        vol_sub = int(vol_sub)

    return vol_sub

for row in database.itertuples(index=True, name='Pandas'):
    index = getattr(row, 'Index')
    date = getattr(row, 'date')

    _open = getattr(row, 'open')
    _open = Decimal(str(_open))

    high = getattr(row, 'high')
    high = Decimal(str(high))

    low = getattr(row, 'low')
    low = Decimal(str(low))

    close = getattr(row, 'close')
    close = Decimal(str(close))

    vol = getattr(row, 'volume')
    vol = Decimal(str(vol))

    ema_close_5 = c_ema_close(5)
    ema_close_21 = c_ema_close(21)
    ema_close_55 = c_ema_close(55)

    vol_sub = c_vol_sub()
    

    ema_open_5 = c_ema_open(5)
    ema_open_21 = c_ema_open(21)
    ema_open_55 = c_ema_open(55)

    oc_chn_5 = c_occhn(ema_close_5, ema_open_5)
    oc_chn_21 = c_occhn(ema_close_21, ema_open_21)
    oc_chn_55 = c_occhn(ema_close_55, ema_open_55)

    cc_chn_5 = c_ccchn(ema_close_5, get_yesterday_ema(5))
    cc_chn_21 = c_ccchn(ema_close_5, get_yesterday_ema(21))
    cc_chn_55 = c_ccchn(ema_close_5, get_yesterday_ema(55))

    cc_dis_3 = c_cc_dis3()
    cc_dis_13 = c_cc_dis13()
    cc_dis_34 = c_cc_dis34()

    candle_o_c = (close - _open) / close * 10000
    candle_up_mind = (hight - max(_open, close)) - abs(_open - close)) / close * 10000
    candle_down_mind = ((min(_open, close)-low)-abs(_open- close))/ close*10000,0)

    edata_row = {
                 'date': date,
                 'index': index,
                 'open': _open,
                 'high': high,
                 'low': low,
                 'close': close,
                 'vol': vol,
                 'ema_close_5': ema_close_5,
                 'ema_close_21': ema_close_21,
                 'ema_close_55': ema_close_55,
                 'ema_open_5': ema_open_5,
                 'ema_open_21': ema_open_21,
                 'ema_open_55': ema_open_55,
                 'oc_chn_5' : oc_chn_5,
                 'oc_chn_21': oc_chn_21,
                 'oc_chn_55': oc_chn_55,
                 'cc_chn_5': cc_chn_5,
                 'cc_chn_21': cc_chn_21,
                 'cc_chn_55': cc_chn_55,
                 'cc_dis_3': cc_dis_3,
                 'cc_dis_13': cc_dis_13,
                 'cc_dis_34': cc_dis_34,
                 }
    # print(edata_row)
    data_edata.append(edata_row)

for datas in data_edata[:3]:
    print(datas)
